package com.fantasybaby.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fantasybaby.bean.User;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static List<User> users = new ArrayList<User>();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("UTF-8");
		String parameter = request.getParameter("id");
		if (parameter != null ) {
			int id = Integer.parseInt(parameter);
			User user = users.get(id);
			request.setAttribute("user", user);
		}
		
		request.getRequestDispatcher("/index.vm").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	@Override
	public void init() throws ServletException {
		User user = new User();
		user.setAddress("邯郸路");
		user.setName("FantasyBaby");
		user.setId(0);
		User user1 = new User();
		user1.setAddress("邯郸路1");
		user1.setName("FantasyBaby1");
		user1.setId(1);
		User user2 = new User();
		user2.setAddress("邯郸路2");
		user2.setName("FantasyBaby2");
		user2.setId(2);
		users.add(user);
		users.add(user1);
		users.add(user2);
		super.init();
	}
}
